<template>
  <share-poster v-if="posterInfo" :typeStr="'spreadCoupon'" :showPoster="true" :posterInfo="posterInfo"></share-poster>
</template>

<script>
import { utilJs } from "@/utils/common.js";
import SharePoster from "./sharePoster.vue"
export default {
  name: 'VoucherShare',
  components: {
    SharePoster
  },
  props: [],
  beforeCreate() {},
  data() {
    return {
      redeemCodeId: '',
      rcdId: '',
      posterInfo: null, //海报信息
      detail: null,
    }
  },
  created() {
  },
  mounted() {},
  computed: {},
  watch: {
  },
  methods: {
    getDetail() {
      utilJs.getMethod(global.apiurl + 'coupon/getJWRedeemCodePage?id=' + this.redeemCodeId + "&rcdId=" + this.rcdId, (res) => {
        if (this.rcdId != '') {
          if (res.userId != -1 && res.userId == localStorage.getItem("userId")) {
            this.isCollectRcId = 2;
          } else if (res.userId != -1 && res.userId != localStorage.getItem("userId")) {
            this.isCollectRcId = 1;
            res.status = 5; //已作废
          }
        }
        this.detail = res;
        let shareUrl = window.location.href.split("#")[0] + "#/homePage/shareholder/shCouponDetail?refereeId=" + localStorage.getItem("userId");
        if (this.redeemCodeId && this.redeemCodeId != '') {
          shareUrl += '&id=' + this.redeemCodeId;
        }
        if (this.rcdId && this.rcdId != '') {
          shareUrl += '&rcdId=' + this.rcdId;
        }
        if (localStorage.getItem("shareholderId")) {
          shareUrl += "&shareholderId=" + localStorage.getItem("shareholderId");
        }
        this.posterInfo = {
          uname: localStorage.getItem("userName"),
          uheadImg: localStorage.getItem("userHeadImg"),
          url: shareUrl,
          price: Number(res.price),
          name: res.name,
          statusStr: res.redeemType == 24 ? '适用指定商品' : res.redeemType == 25 ? '适用商城商品' : res.redeemType == 26 ? '适用所有推广课程' : '',
          timeStr: res.validStatus == 1 ? "领券当日起" + res.validPeriod + '天内可用' : (res.validStatus == 2 ? "领券第二日起" + res.validPeriod + '天内可用' : (res.startAt.split(" ")[0].replace(/-/g, '.') + "-" + res.endAt.split(" ")[0].replace(/-/g, '.')))
        }
        this.wechatShare();
      });
    },
    //分享
    wechatShare: function () {
      let shareUrl =
        window.location.href.split("#")[0] +
        "#/homePage/shareholder/shCouponDetail?refereeId=" +
        localStorage.getItem("userId");
      if (this.redeemCodeId && this.redeemCodeId != '') {
        shareUrl += '&id=' + this.redeemCodeId;
      }
      if (this.rcdId && this.rcdId != '') {
        shareUrl += '&rcdId=' + this.rcdId
      }
      if (localStorage.getItem("shareholderId")) {
        shareUrl += "&shareholderId=" + localStorage.getItem("shareholderId");
      }
      let title = "送你一张优惠券";
      let shareDesc = "";
      if (this.detail.enableShare == -1) {
        utilJs.wechatConfig(shareUrl, title, "", shareDesc, function () { }, true, { hideAllNonBaseMenuItem: 1 });
      } else {
        utilJs.wechatConfig(shareUrl, title, "", shareDesc, function () { }, true, { hideAllNonBaseMenuItem: 0 });
      }
    },
    //手机端分享
    appShare: function () {
      let shareUrl =
        window.location.href.split("#")[0] +
        "#/homePage/shareholder/shCouponDetail?refereeId=" +
        localStorage.getItem("userId");
      if (this.redeemCodeId && this.redeemCodeId != '') {
        shareUrl += '&id=' + this.redeemCodeId;
      }
      if (this.rcdId && this.rcdId != '') {
        shareUrl += '&rcdId=' + this.rcdId;
      }
      if (localStorage.getItem("shareholderId")) {
        shareUrl += "&shareholderId=" + localStorage.getItem("shareholderId");
      }
      let title = "送你一张优惠券";
      let shareDesc = "";
      if (this.detail.enableShare == -1) {
        utilJs.appShareTrue(title, "", shareUrl, shareDesc, function () { }, true, { hideAllNonBaseMenuItem: 1 });
      } else {
        utilJs.appShareTrue(title, "", shareUrl, shareDesc, function () { }, true, { hideAllNonBaseMenuItem: 0 });
      }
    },
  },
  activated() {
    this.posterInfo = null;
    this.detail = null;
    this.redeemCodeId = this.$route.query.id || '';
    this.rcdId = this.$route.query.rcdId || '';
    this.getDetail();
  },
  deactivated() {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
